package com.aidex.system.service.impl;

import com.aidex.common.core.service.impl.BasicServiceImpl;
import com.aidex.common.exception.BizException;
import com.aidex.common.exception.CustomException;
import com.aidex.framework.cache.DictUtils;
import com.aidex.system.common.SysErrorCode;
import com.aidex.system.domain.SysDictData;
import com.aidex.system.domain.SysDictType;
import com.aidex.system.mapper.SysDictDataMapper;
import com.aidex.system.mapper.SysDictTypeMapper;
import com.aidex.system.service.SysDictTypeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

@Service
public class SysDictTypeServiceImpl extends BasicServiceImpl<SysDictTypeMapper, SysDictType> implements SysDictTypeService {

    @Autowired(required = false)
    private SysDictDataMapper sysDictDataMapper;

    @Override
    public int insert(SysDictType dictType) {
        checkDictTypeUnique(dictType);
        int flag = super.insert(dictType);
        if (flag > 0) {
            DictUtils.clearDictCache();
        }
        return flag;
    }

    @Override
    public int updateById(SysDictType dictType) {
        checkDictTypeUnique(dictType);
        int flag = super.updateById(dictType);
        if (flag > 0) {
            DictUtils.clearDictCache();
        }
        return flag;
    }

    @Override
    public int deleteById(Serializable id) {
        SysDictType entity = selectById(id);
        if(entity != null) {
            int flag = super.deleteById(id);
            if (flag > 0) {
                DictUtils.clearDictCache();
                QueryWrapper<SysDictData> wrapper = new QueryWrapper<>();
                wrapper.eq("dict_type", entity.getDictType());
                if (sysDictDataMapper.selectCount(wrapper) > 0) {
                    throw new CustomException(String.format("%1$s存在字典信息,不能删除", entity.getDictName()));
                }
            }
            return flag;
        }
        return 0;
    }

    @Override
    public int deleteBatchIds(Collection<? extends Serializable> idList) {
        int flag = 0;
        if (idList != null && !idList.isEmpty()) {
            for (Serializable id : idList) {
                flag += deleteById(id);
            }
        }
        return flag;
    }

    @Override
    public void refreshCache() {
        DictUtils.clearDictCache();
        loadingDictCache();
    }

    @Override
    public void checkDictTypeUnique(SysDictType dict) {
        QueryWrapper<SysDictType> wrapper = new QueryWrapper<>();
        wrapper.eq("dict_type", dict.getDictType());
        wrapper.ne("id", dict.getId());
        if (mapper.selectCount(wrapper) > 0) {
            throw new BizException(SysErrorCode.B_SYSDICTTYPE_DictTypeAlreadyExist);
        }
    }

    @Override
    public void loadingDictCache() {
        List<SysDictType> sysDictTypeList = selectList(new QueryWrapper<>());
        for (SysDictType sysDictType : sysDictTypeList) {
            DictUtils.setDictCache(sysDictType.getDictType());
        }
    }
}
